package com.tuteng.administrator.ashospital.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.tuteng.administrator.ashospital.Activity.Lis_JydjActivity;
import com.tuteng.administrator.ashospital.Activity.Lis_mxActivity;
import com.tuteng.administrator.ashospital.AdapterManger.FriendAdapter;
import com.tuteng.administrator.ashospital.R;
import com.tuteng.administrator.ashospital.beans.FriendInfoBean;

import java.io.Serializable;
import java.util.List;

/**
 * 自定义ListDialog,选择就诊人对话框
 * 黄启位
 */
public class CommomListDialog extends Dialog implements View.OnClickListener{
    private TextView titleTxt;
    private List<FriendInfoBean> datas;
   private ListView mListView;
    private Context mContext;
    private OnCloseListener listener;
    private String title;
    private Class toActivity;
    private FriendAdapter adapter;
    public CommomListDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    public CommomListDialog(Context context, int themeResId,List<FriendInfoBean> datas,Class toActivity) {
        super(context, themeResId);
        this.toActivity=toActivity;
        this.datas=datas;
        this.mContext = context;
    }

    protected CommomListDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    public CommomListDialog setTitle(String title){
        this.title = title;
        return this;
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_list_commom);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView(){

        mListView=(ListView)findViewById(R.id.dialog_list_friendinfo);
        adapter=new FriendAdapter(mContext,datas);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dismiss();
                FriendInfoBean bean=new FriendInfoBean();
                bean=datas.get(position);
                goToActivity(mContext,toActivity,bean);

            }
        });
        titleTxt = (TextView)findViewById(R.id.title);
        if(!TextUtils.isEmpty(title)){
            titleTxt.setText(title);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cancel:
                if(listener != null){
                    listener.onClick(this, false);
                }
                this.dismiss();
                break;
            case R.id.submit:
                if(listener != null){

                    listener.onClick(this, true);
                }
                break;
        }
    }

    public interface OnCloseListener{
        void onClick(Dialog dialog, boolean confirm);
    }


    /**
     * 跳转Activity
     * @param context
     * @param toActivity
     * @param mda
     */
    private void goToActivity(Context context, Class toActivity,FriendInfoBean mda) {

        Intent intent=new Intent();
        Bundle bundle1=new Bundle();
          intent.setClass(context,toActivity);
        bundle1.putSerializable("cardNo",mda.getCardNo());
        intent.putExtras(bundle1);
        //   intent.putExtra("info", mda);
       context.startActivity(intent);
    }

}
